﻿using System;
using System.Collections.Generic;
using System.Linq;
using NotifyPilot.Models;
using NotifyPilot.WCF.Helpers;
using NotifyPilot.WCF.NotifyClients;

namespace NotifyPilot.Services
{
    public class ServicesConfigurationServices
    {
   
        private IList<ServicesConfiguration> _servicesConfigurations;

        public IList<ServicesConfiguration> ServicesConfigurations
        {
            get
            {
                return _servicesConfigurations ??
                    (_servicesConfigurations = this.initServicesConfigurations());
            }
        }

        public Boolean SaveServicesConfiguration(IList<ServicesConfiguration> services)
        {
            return Helpers.SerializeObjectToFile(services, ConfigPath.PathServices);
        }

        private IList<ServicesConfiguration> initServicesConfigurations()
        {
            var knowServices = Helpers.GetNotifyClientClasses(typeof(INotifyClient)) as Type[];
            if (knowServices == null)
                return null;

            var line = new List<EventsClass>
                       {
                           new EventsClass
                           {
                               EventType = EventType.WorkItemChangedEvent,
                               Use = false
                           },
                            new EventsClass
                           {
                              EventType = EventType.CheckinEvent,
                               Use = false
                           },
                            new EventsClass
                           {
                              EventType = EventType.Builds,
                               Use = false
                           }
                       };

            // Read the config file
            var servicesList = Helpers.DeserialiseXmlFromFile<List<ServicesConfiguration>>(ConfigPath.PathServices);
            if (servicesList != null && servicesList.Any())
            {
                // If it's find check if there's not noex services in app
                var servicesNameList = servicesList.Select(s => s.ServiceName).ToArray();
                var unknowServices = knowServices.Where(p => !servicesNameList.Contains(p.Name)).ToArray();
                if (unknowServices.Any())
                {
                    // If something is new
                    servicesList.AddRange(unknowServices.Select(type => new ServicesConfiguration
                    {
                        ServiceName = type.Name,
                        EventsClasses = line
                    }));
                }
                return servicesList;
            }

            // if there is not config file, create an empty list
            var aRet = new List<ServicesConfiguration>(knowServices.Count());
            aRet.AddRange(knowServices.Select(service => new ServicesConfiguration
            {
                ServiceName = service.Name,
                EventsClasses = line
            }));
            return aRet;
        }
    }
}